<?php
namespace App\Tools;
use App\Http\Controllers\Controller;
use QL\QueryList;
use GuzzleHttp\Client;
use QL\Ext\PhantomJs;

class Collection extends Controller{

	protected $queryObj = null; //querylist 对象
	protected $guzzleObj = null; // guzzle对象
	protected $searchUrl = ''; //小说网站搜索链接
	protected $is_book = true; //书籍是否存在

	public function __construct(){
		if(is_null($this->queryObj)){
			$this->queryObj = new QueryList;
		}

		if(is_null($this->guzzleObj)){
			$this->guzzleObj = new Client;
		}
		
	}

	// 采集起点
	public function qidian($keyword){

		//基本结构
		$query = $this->queryObj;
		$this->searchUrl = 'https://www.qidian.com/search?kw=';
		$html = $this->getWebHtml($this->searchUrl.urlencode($keyword));

		// 搜查规则
		$rule = [
			'name'=>['.book-mid-info:first h4','text'],//
			'bookId'=>['.book-mid-info:first h4 a','data-bid'],//
		];
		$res = $query->html($html)->rules($rule)->query()->getData();

		// 判断是否搜查到
		if(empty($res) || $res[0]['name'] != $keyword){
			$this->is_book = false;
		}

		if($this->is_book != true){
			return false;
		}

		//获取书籍详细信息
		$bookUrl = "https://book.qidian.com/info/";
		$html = $this->getWebHtml($bookUrl.$res[0]['bookId'].'#Catalog');

		// 详细书籍规则
		$rule = [
			'name'=>['.book-info:first h1 em:first','text'],//书名
			'author'=>['.book-info:first .writer','text'],//作者
			'introduction'=>['.book-info:first .intro','text'],//简介
			'book_img'=>['#bookImg img','src'],//书图
			'words_num'=>['.book-info:first p em:first','text'],//字数
			// 'category'=>['#j-catalogWrap ul li a','text'],//章节
		];
		$res = $query->html($html)->rules($rule)->query()->getData();
		$res['source'] = 'qidian'; //源头
		return $res;
	}




	/*******
	/* 
	/*  追书神器api start
	/*  https://github.com/xiadd/zhuishushenqi
	/*  author:hg 2017/12/12
	/*
	*******/

	//获取搜索数据
	public function zsSearch($keyword){
		$url = 'http://novel.juhe.im/search?keyword='.urlencode($keyword);
		return $this->guzzleFun($url);
	}

	// 根据id获取书籍详情
	public function zsBookInfo($id){
		$url = 'http://novel.juhe.im/book-info/'.$id;
		return $this->guzzleFun($url);
	}

	// 获取该作者名下的所有书籍
	public function zsAuthorAllBook($author){
		$url = 'http://novel.juhe.im/author-books?author='.urlencode($author);
		return $this->guzzleFun($url);
	}

	// 获取该书籍所有章节 $id 是根据源而来
	public function zsBookChapter($id){
		$url = 'http://novel.juhe.im/book-info/'.$id;
		return $this->guzzleFun($url);
	}

	// 获取该书籍某指定章节详情
	public function zsBookChapterInfo($url){
		$url = 'http://novel.juhe.im/chapters/'.urlencode($url);
		return $this->guzzleFun($url);
	}

	// 获取该书籍源
	public function zsBookSource($id){
		$url = 'http://novel.juhe.im/book-sources?view=summary&book='.$id;
		return $this->guzzleFun($url);
	}

	// 获取栏目
	public function zsCat(){
		$url = 'http://novel.juhe.im/categories';
		return $this->guzzleFun($url);
	}

	/*******
	/* 
	/*  追书神器api end
	/*  https://github.com/xiadd/zhuishushenqi
	/*  author:hg 2017/12/12
	/*
	*******/


	// 获取指定页面数据的html
	protected function getWebHtml($url){
		// $this->guzzleObj = new Client;
		return $this->guzzleFun($url);
	}

	protected function guzzleFun($url){
		$this->guzzleObj->get($url)->getBody()->getContents();
	}

	protected function phantomJs($url){
		$ql = QueryList::getInstance();
		// 安装时需要设置PhantomJS二进制文件路径
		$ql->use(PhantomJs::class,'/soft/phantomjs-2.1.1-linux-x86_64/bin/phantomjs');
		//or Custom function name
		$ql->use(PhantomJs::class,'/soft/phantomjs-2.1.1-linux-x86_64/bin/phantomjs','browser');
		$html = $ql->browser($url)->getHtml();
		return $html;
	}

}